<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="接单时间">
        <el-date-picker
          v-model="daterangeReceiveTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="orderList[1]">
      <template v-for="(item,index) in orderList[0]">
        <el-table-column :prop="item.column_name" :label="item.column_name" :key="index"></el-table-column>
      </template>
    </el-table>
    <div id="chart" style="height:500px;width:100%" />
  </div>
</template>

<script>
import {
  operateTime
} from "@/api/business/order";
import echarts from "echarts";

export default {
  name: "Order",
  data() {
    return {
      // 单据状态时间范围
      daterangeReceiveTime: [],
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      // 工单管理表格数据
      orderList: [],
      // 查询参数
      queryParams: {
        receiveTime: null
      },
      yData: [],
      xData1: [],
      xData2: [],
      xData3: [],
      xData4: []
    };
  },
  created() {
    this.initDateScope();
    this.getList();
    this.initChart();
  },
  methods: {
    /** 初始化日期范围 */
    initDateScope() {
      let end = new Date();
      let year = end.getFullYear();
      let month = end.getMonth() + 1;//0-11表示1-12月
      let day = end.getDate();
      let dateObj = {};
      dateObj.end = year + '-' + month + '-' + day;
      let endMonthDay = new Date(year, month, 0).getDate();    //当前月的总天数
      if(month - 1 <= 0){ //如果是1月，年数往前推一年<br>　　　　
        dateObj.start = (year - 1) + '-' + 12 + '-' + day;
      }else{
        let startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();
        if(startMonthDay < day){    //1个月前所在月的总天数小于现在的天日期
          if(day < endMonthDay){        //当前天日期小于当前月总天数
            dateObj.start = year + '-' + (month - 1) + '-' + (startMonthDay - (endMonthDay - day));
          }else{
            dateObj.start = year + '-' + (month - 1) + '-' + startMonthDay;
          }
        }else{
          dateObj.start = year + '-' + (month - 1) + '-' + day;
        }
      }
      this.daterangeReceiveTime.push(dateObj.start)
      this.daterangeReceiveTime.push(dateObj.end)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
      this.initChart();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeReceiveTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询工单管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeReceiveTime && '' != this.daterangeReceiveTime) {
        this.queryParams.params["startTime"] = this.daterangeReceiveTime[0];
        this.queryParams.params["endTime"] = this.daterangeReceiveTime[1];
      }
      operateTime(this.queryParams).then(response => {
        this.orderList = response.rows;
        let xList = this.orderList[1]
        let xList1 = xList[0]
        for (let i in xList1) {
          if (i != '合计' && i != '姓名') {
            this.xData1.push(xList1[i])
          }
        }
        let xList2 = xList[1]
        for (let i in xList2) {
          if (i != '合计' && i != '姓名') {
            this.xData2.push(xList2[i])
          }
        }
        let xList3 = xList[2]
        for (let i in xList3) {
          if (i != '合计' && i != '姓名') {
            this.xData3.push(xList3[i])
          }
        }
        let xList4 = xList[3]
        for (let i in xList4) {
          if (i != '合计' && i != '姓名') {
            this.xData4.push(xList4[i])
          }
        }
        let yList = this.orderList[0]
        for (let i = 1; i < yList.length; i++) {
          let colName = yList[i].column_name;
          this.yData.push(colName)
        }
        this.initChart();
        this.loading = false;
      });
    },
    initChart() {
      this.chart = echarts.init(document.getElementById('chart'))
      this.setOptions(this.chartData)
    },
    setOptions() {
      this.chart.setOption({
        title: {
          text: '工单处理实效统计'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {},
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          boundaryGap: [0, 0.01]
        },
        yAxis: {
          type: 'category',
          data: this.yData
        },
        series: [
          {
            name: '平均时效（分钟）',
            type: 'bar',
            data: this.xData4
          },
          {
            name: '最大时效（分钟）',
            type: 'bar',
            data: this.xData3
          },
          {
            name: '最小时效（分钟）',
            type: 'bar',
            data: this.xData2
          },
          {
            name: '工单数量（个）',
            type: 'bar',
            data: this.xData1
          }
        ]
      })
    }
  }
}
;
</script>
